<?php

require_once LIBRARIES_ENTITIES_DATABASE;
require_once LIBRARIES_ENTITIES_CONTACTS;


require_once LIBRARIES_TOOLS_UTILITY;
require_once LIBRARIES_TOOLS_XTEMPLATE;
require_once LIBRARIES_TOOLS_PAGERBACKEND;

class ContactController {

    public function invoke() {

        // function list
        $groupFunctions = $_SESSION['groupFunctions'];

        // directory path
        $dir = dirname(__FILE__);

        $action = '';
        if (isset($_POST['hdAction'])) {
            $action = $_POST['hdAction'];
        }



        $task = '';
        if (isset($_GET['task'])) {
            $task = $_GET['task'];
        }


        $rowToView = 10;
        if (isset($_POST["limit"])) {
            $rowToView = $_POST["limit"];
        }

        // serID
        $userID = $_SESSION["session"]["sUserID"];


        switch ($action) {
            case 'remove':
                $values = $_POST["cid"];
                $result = $this->deleteContact($values);
                if ($result == OK) {
                    Utility::messageBox('Xóa thành công', './?com=com_contact&task=view');
                }
                break;
        }






        if ($task == 'view') {
            // check view permission
            if (Utility::checkPermission('view', $groupFunctions, CONTACT_MANAGEMENT)) {
                $viewFile = $dir . '/views/contact_list.html';
                $xtemplate = new XTemplate($viewFile);

                // CHECK PERMISSION
                // check delete button
                if (Utility::checkPermission('delete', $groupFunctions, CONTACT_MANAGEMENT)) {
                    $xtemplate->parse('main.delete_button');
                }

                $indexCheckBox = 1;

                $rurrentPage = 1;
                if (isset($_POST["page"])) {
                    $rurrentPage = $_POST["page"];
                }

                // chỉ mục (số thứ tự của bài viết) bài viết
                $index = 1;

                $contacts = $this->getContactList();

                $pager = PagerBackEnd::getPagerData(count($contacts), $rowToView, $rurrentPage);


                if ($contacts != null) {

                    $indexCheckBox = 1;

                    foreach ($contacts as $contact) {

                        if ($index > $pager->offset && $index <= $pager->limit) {

                            $xtemplate->assign('INDEX', $index);
                            $xtemplate->assign('INDEX_CHECKBOX', $indexCheckBox);

                            $xtemplate->assign('ID', $contact->getID());
                            $xtemplate->assign('FULLNAME', $contact->getFullname());
                            $xtemplate->assign('EMAIL', $contact->getEmail());
                            $xtemplate->assign('COMPANY', $contact->getCompany());
                            $xtemplate->assign('FAX', $contact->getFax());
                            $xtemplate->assign('TITLE', $contact->getTitle());
                            $xtemplate->assign('CONTENT', $contact->getContent());
                            $xtemplate->assign('ADDRESS', $contact->getAddress());
                            $xtemplate->assign('PHONE', $contact->getPhone());
                            $xtemplate->assign('CREATED_DATE', Utility::formatDateTimeToView($contact->getCreatedDate()));
                            $xtemplate->parse('main.row');

                            $indexCheckBox++;
                        }
                        $index++;
                    }
                }

                $xtemplate->assign('LIMIT_LIST', Utility::getLimitList($rowToView));
                $xtemplate->assign('ROW_TO_VIEW', $rowToView);
                $xtemplate->assign('CURRENT_PAGE', $rurrentPage);
                $xtemplate->assign('AMOUNT_OF_PAGE', $pager->numPages);
                $xtemplate->assign('PAGING_HTML', $pager->paging);
                $xtemplate->parse('main');
                $xtemplate->out('main');
            } else {
                echo ERROR_PERMISSION;
            }
        }
    }

    private function getContactList() {
        $db = new Database();
        try {
            $contact = new Contacts();
            $result = $contact->getList($db);
            $db->submitChanges();
            return $result;
        } catch (Exception $exc) {
            echo $exc->getMessage();
            $db->rollback();
            exit();
        }
    }

    private function deleteContact($values) {
        $db = new Database();
        try {
            for ($i = 0; $i < count($values); $i++) {
                $contact = new Contacts();
                $contact->setID($values[$i]);
                $contact->delete($db);
            }
            $db->submitChanges();
            return OK;
        } catch (PDOException $exc) {
            echo $exc->getMessage();
            $db->rollback();
            exit();
        }
    }

}

?>
